package switchviz.network.netswitch;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import switchviz.core.ResourceManager;
import switchviz.gui.component.IListener;
import switchviz.gui.component.JLabeldComponent;
import switchviz.gui.component.JMaskedTextField;
import switchviz.gui.component.PaddingPanel;
import switchviz.network.snmp.SnmpWalkerFrame;

public class SwitchConfigurationPanel extends PaddingPanel implements ActionListener
{

	/**
	 * 
	 */
	private static final long serialVersionUID = -3236321345379689789L;
	private JLabeldComponent _txtname;
	private JLabeldComponent _txtaddress;
	private JMaskedTextField _txtport;
	private JLabeldComponent _txtdescr;
	private JLabeldComponent _txtcommunity;
	private JMaskedTextField _txttimeout;
	private JMaskedTextField _txtmaxretries;
	private JMaskedTextField _txtpducapacity;
	private JLabeldComponent _lblportnumber;
	private JLabeldComponent _txttype;
	private JButton _butttest;
	private JButton _buttapply;
	private JButton _buttport;
	private JButton _buttsnmpwalk;
	private SwitchDataModel _datamodel = null;
	
	public void setValues(SwitchDataModel datamodel)
	{
		_datamodel = datamodel;
		_txtname.setText(_datamodel.getName());
		_txtaddress.setText(_datamodel.getAddress());
		_txtport.setText(String.valueOf(_datamodel.getPort()));
		_txtdescr.setText(_datamodel.getDescription());
		_txtcommunity.setText(_datamodel.getCommunity());
		_txttimeout.setText(String.valueOf(_datamodel.getTimeout()));
		_txtmaxretries.setText(String.valueOf(_datamodel.getMaxRetries()));
		_txtpducapacity.setText(String.valueOf(_datamodel.getPDUCapacity()));
		_txttype.setText(_datamodel.getType());
		_lblportnumber.setText(String.valueOf(_datamodel.getPortList().size()));
		_txtdescr.getTextArea().setCaretPosition(0);
	}
	
	public SwitchDataModel getValues()
	{
		getSwitchModel().setName(_txtname.getText());
		getSwitchModel().setAddress(_txtaddress.getText());
		getSwitchModel().setPort(Integer.valueOf(_txtport.getText()));
		getSwitchModel().setDescription(_txtdescr.getText());
		getSwitchModel().setCommunity(_txtcommunity.getText());
		getSwitchModel().setTimeout(Long.valueOf(_txttimeout.getText()));
		getSwitchModel().setMaxRetries(Integer.valueOf(_txtmaxretries.getText()));
		getSwitchModel().setPDUCapacity(Integer.valueOf(_txtpducapacity.getText()));
		return getSwitchModel();
	}
	
	public SwitchConfigurationPanel()
	{
		this.initialize();
	}
	
	public void addListener(IListener l)
	{
		_butttest.addActionListener(l);
		_buttapply.addActionListener(l);
		_buttport.addActionListener(l);
	}
	
	public void firstConnectInitialize()
	{
		_txttype.setText("Getting Type Information... ");
		_lblportnumber.setText("Getting Port Information... ");
	}
	
	public void enableApplyButton(boolean state)
	{
		_buttport.setEnabled(state);
	}
	
	public void testEnableButtons()
	{
		if (_txtaddress.getText().trim().equals("") || _txtport.getText().trim().equals(""))
		{
			_butttest.setEnabled(false);
			_buttport.setEnabled(false);
		}
		else
		{
			_butttest.setEnabled(true);
			_buttport.setEnabled(true);
		}
	}
	
	private SwitchDataModel getSwitchModel()
	{
		if (_datamodel == null)
		{
			_datamodel = new SwitchDataModel();
		}
		return _datamodel;
	}
	
	
	private void initialize()
	{
		
		_txtname = new JLabeldComponent("Name: ", JLabeldComponent.TEXTFIELD);
        _txtname.getTextField().setColumns(20);
        _txtaddress = new JLabeldComponent("Address: ", JLabeldComponent.TEXTFIELD);
        _txtaddress.getTextField().setColumns(20);
        _txtport = new JMaskedTextField("Port: ", JMaskedTextField.INTNUM);
        _txtport.setMax(65535);
        _txtport.setMin(1);
        _txtport.setText("0");
        _txttimeout = new JMaskedTextField("Timeout: ", JMaskedTextField.INTNUM);
        _txttimeout.setMax(3600000);
        _txttimeout.setMin(0);
        _txttimeout.setText("0");
        _txtcommunity = new JLabeldComponent("Community: ", JLabeldComponent.TEXTFIELD);
        _txtcommunity.getTextField().setColumns(20);
        _txtmaxretries= new JMaskedTextField("Max Retries: ", JMaskedTextField.INTNUM);
        _txtmaxretries.setMax(60);
        _txtmaxretries.setMin(0);
        _txtmaxretries.setText("0");
        _txtpducapacity = new JMaskedTextField("PDU Capacity: ", JMaskedTextField.INTNUM);
        _txtpducapacity.setMax(400);
        _txtpducapacity.setMin(1);
        _txtpducapacity.setText("0");
        _txtdescr = new JLabeldComponent("Description: ", JLabeldComponent.TEXTAREA);
        _lblportnumber = new JLabeldComponent("Number of Ports: ", JLabeldComponent.LABEL);
        _lblportnumber.setText("0");
        _txttype = new JLabeldComponent("Switch Type: ", JLabeldComponent.TEXTAREA);
        _txttype.getTextArea().setEditable(false);
        
        _butttest = new JButton("Query Switch");
        _butttest.setActionCommand(ResourceManager.getInstance().getActionTest());
        _butttest.setEnabled(false);
        _buttport = new JButton("Reload Port Info");
        _buttport.setActionCommand(ResourceManager.getInstance().getActionGetData());
        _buttport.setEnabled(false);
        _buttapply = new JButton("Apply Changes");
        _buttapply.setActionCommand(ResourceManager.getInstance().getActionApply());
        _buttsnmpwalk = new JButton("SNMP Walk");
        _buttsnmpwalk.addActionListener(this);
        
        JPanel buttonpanel = new JPanel();
        buttonpanel.setLayout(new BoxLayout(buttonpanel, BoxLayout.LINE_AXIS));
        buttonpanel.add(Box.createHorizontalGlue());
        buttonpanel.add(_buttapply);
        buttonpanel.add(_buttport);
        buttonpanel.add(_butttest);
        buttonpanel.add(_buttsnmpwalk);
        
        PaddingPanel confpane = new PaddingPanel();
        confpane.setLayout(new BoxLayout(confpane, BoxLayout.PAGE_AXIS));
        confpane.setBorder(BorderFactory.createLoweredBevelBorder());
        
        confpane.add(_txtname);
        confpane.add(_txttype);
        confpane.add(_txtaddress);
        confpane.add(_txtport);
        confpane.add(_txtcommunity);
        confpane.add(_txttimeout);
        confpane.add(_txtmaxretries);
        confpane.add(_txtpducapacity);
        confpane.add(_lblportnumber);
        confpane.add(_txtdescr);
        confpane.add(new JLabel(" "));
        
        this.setLayout(ResourceManager.getInstance().getBorderLayout());
        this.add(confpane, BorderLayout.CENTER);
        this.add(buttonpanel, BorderLayout.SOUTH);
	}


	public void actionPerformed(ActionEvent evt) 
	{
		new SnmpWalkerFrame(getSwitchModel());
	}
}
